﻿#region  Copyright (C) 2013 Unisys Corporation

// =============================================================================================
// All Rights Reserved. This work contains trade secrets and confidential material of UNISYS, 
// and its use or disclosure in whole or in part without explicit written permission of 
// UNISYS is prohibited. 
//  
// Product Name          :  Unisys e-Workflow and Imaging
// File Name             :  SqlDataClass.cs
// Authored By           :  Unisys Corporation
// =============================================================================================
//	Modified by                         Date            Version
// =============================================================================================
//	ronald.aguilar@cr.unisys.com      May 01, 2013    Initial Version
// =============================================================================================
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data;
using Entidades;

namespace Datos.Data_Base
{
    public class SqlDataClass
    {        
        private SqlConnection sqlConnection { get; set; }


        public void OpenConnection()
        {
            try 
            {
             
                sqlConnection = new SqlConnection(a.a(Configuration.stringConnection));

                sqlConnection.Open();
                
            }
            catch (SqlException sqlExc)
            {
                throw (sqlExc);
            }
        }

      
        public void CloseConnection()
        {
            try
            {
                sqlConnection.Close();
                sqlConnection.Dispose();
            }
            catch (SqlException sqlExc)
            {
                throw (sqlExc);   
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public int SQLUpdate(string objectID,string version,string historico,bool openConnection)
        {
            try
            {
                if(openConnection)
                    this.OpenConnection();
                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = sqlConnection;
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = Configuration.updateCommand;


                SqlParameter objID = sqlCommand.Parameters.Add("@"+Configuration.updateIDParameter, SqlDbType.VarChar);
                objID.Direction = ParameterDirection.Input;
                objID.Value = objectID;

                SqlParameter VERSION_WORKITEM = sqlCommand.Parameters.Add("@"+Configuration.updateVersionParameter, SqlDbType.VarChar);
                VERSION_WORKITEM.Direction = ParameterDirection.Input;
                VERSION_WORKITEM.Value = version;

                SqlParameter HISTORICO = sqlCommand.Parameters.Add("@"+Configuration.updateHistoricoParameter, SqlDbType.VarChar);
                HISTORICO.Direction = ParameterDirection.Input;
                HISTORICO.Value = historico;

                int result = sqlCommand.ExecuteNonQuery();
                if(openConnection)
                    this.CloseConnection();

                return result;
                
            }
            catch (SqlException ex)
            {
                this.CloseConnection();
                throw (ex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public SqlDataReader SQLRead(string query)
        {
            try
            {                
                this.OpenConnection();
                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = sqlConnection;
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = Configuration.readCommand;


                SqlParameter parameter = sqlCommand.Parameters.Add("@"+Configuration.readCommandParameter, SqlDbType.VarChar);
                parameter.Direction = ParameterDirection.Input;
                parameter.Value = query;

                return sqlCommand.ExecuteReader(CommandBehavior.CloseConnection); //retorna el resultado del store procedure               
            }
            catch (SqlException ex)
            {
                this.CloseConnection();
                throw (ex);
            }
        }
    }
}
